home *** CD-ROM | disk | FTP | other *** search
- #include <libc.h>
- #include <osfcn.h>
- #include <iostream.h>
- #include <errno.h>
-
- #include "Transport.h"
- #include <RJS/Util.h>
-
- void RJS_Socket::dump()
- {
- cout << "Socket::dump() { " << endl;
- cout << " Protocol : " << protocol <<endl;
- cout << " Family : " << family <<endl;
- cout << " Type : " << type <<endl;
- cout << " td : " << td << endl;
- cout << "}" << endl;
-
- }
-
- int RJS_Socket::check_syscall(int stat, char *mess)
- {
- if (stat==-1) {
- ss_set(RJS_Status(errno));
- if (return_on_error) return 0;
- dump();
- RJS_Util::crash_and_burn("Socket",mess,ss_message());
- } else ss_set(SS_success);
- return 1;
- }
-
- int RJS_Socket::syscall(int stat, char *mess)
- {
- if (stat==-1) {
- ss_set(RJS_Status(errno));
- if (return_on_error) return stat;
- dump();
- RJS_Util::crash_and_burn("RJS_Socket",mess,ss_message());
- } else ss_set(SS_success);
- return stat;
- }
-
- RJS_Socket::RJS_Socket(AddressFamily af, SocketType st)
- {
- type = st; // socket type
- family = af; // address family
- protocol = 0; // protocol
- sex = Unknown;
- }
-
-
- RJS_Socket::RJS_Socket(AddressFamily af) // protected, only used from derived
- { // class constructor
- family = af; // address family
- protocol = 0; // protocol
- sex = Unknown;
- }
-
- RJS_Socket::RJS_Socket(SocketType st) // protected, only used from derived
- { // class constructor
- type = st; // socket type
- protocol = 0; // protocol
- sex = Unknown;
- }
-
- int RJS_Socket::socket()
- {
- td = ::socket(address_family(),socket_type(),0);
- return check_syscall(td,"socket");
- }
-
- int RJS_Socket::accept(struct sockaddr *sa,int &len)
- {
- return syscall(::accept(td,sa, &len),"accept");
- }
-
- int RJS_Socket::bind(const struct sockaddr *sa, int sa_len)
- {
- return check_syscall(::bind(td,sa,sa_len),"bind");
- }
-
- int RJS_Socket::connect(const struct sockaddr *sa, int sa_len)
- {
- return check_syscall(::connect(td,sa,sa_len),"connect");
- }
-
- int RJS_Socket::getpeername(struct sockaddr *sa, int &sa_len)
- {
- return check_syscall(::getpeername(td,sa,&sa_len),"getpeername");
- }
-
- int RJS_Socket::getsockname(struct sockaddr *sa, int &sa_len)
- {
- return check_syscall(::getsockname(td,sa,&sa_len),"getsockname");
- }
-
- int RJS_Socket::recv(char *buffer,int maxbuf,MsgReceiveFlags mrf)
- {
- return syscall(::recv(td,buffer,maxbuf,int(mrf)),"recv");
- }
-
- int RJS_Socket::recvfrom(char *buffer,int maxbuf,struct sockaddr *sa,
- int &len,MsgReceiveFlags mrf)
- {
- return syscall(::recvfrom(td,buffer,maxbuf,int(mrf),sa,&len),"recvfrom");
- }
-
- int RJS_Socket::recvmsg(struct msghdr *mh,MsgReceiveFlags mrf)
- {
- return syscall(::recvmsg(td,mh,int(mrf)),"recvmsg");
- }
-
- int RJS_Socket::send(const char *buffer,int num,MsgSendFlags msf)
- {
- return syscall(::send(td,buffer,num,int(msf)),"send");
- }
-
-
- int RJS_Socket::sendto(const char *buffer,int num,
- const struct sockaddr *sa, int len,MsgSendFlags msf)
- {
- return syscall(::sendto(td,buffer,num,int(msf),sa,len),"sendto");
- }
-
- int RJS_Socket::sendmsg(struct msghdr *mh,MsgSendFlags msf)
- {
- return syscall(::sendmsg(td,mh,int(msf)),"sendmsg");
- }
-
- int RJS_Socket::listen(int max_conn)
- {
- return check_syscall(::listen(td,max_conn),"listen");
- }
-
-
- RJS_Socket::~RJS_Socket()
- {
- if (inuse()) close();
- }
-
- void RJS_Socket::linger()
- {
- if (!inuse()) return;
- static struct linger ling = {1,10};
- int stat=::setsockopt(td,SOL_SOCKET,SO_LINGER,(const char *)&ling,sizeof(ling));
- if (stat==-1) {
- ss_set(RJS_Status(errno));
- if (return_on_error) return;
- RJS_Util::crash_and_burn("RJS_Socket","linger",ss_message());
- }
- ss_set(SS_success);
-
- }
-
- int RJS_Socket::setsockopt(SocketLevelOption slo, FlagOption fo)
- {
- if (!inuse()) return 0;
- int flag=fo;
- return check_syscall(::setsockopt(td,SOL_SOCKET,int(slo),
- (const char *) &flag,sizeof(flag)),"setsockopt");
- }
-
- int RJS_Socket::getsockopt(SocketLevelOption slo,FlagOption &fo)
- {
- if (!inuse()) return 0;
- int flag; int flagsize=sizeof(flag);
- int stat=check_syscall(::getsockopt(td,SOL_SOCKET,int(slo),
- (char *) &flag,&flagsize),"getsockopt");
- if (flag) fo=On; else fo=Off;
- return stat;
-
- }
-
- void RJS_Socket::shutdown(ShutDownType sdt)
- {
- if (!inuse()) return;
- check_syscall(::shutdown(td,sdt),"shutdown");
- }
-